#include "MainWindow.h"
#include "ui_MainWindow.h"


// widgets
#include "CampaignMainWindow.h"


// Qt
#include <QFileDialog>
#include <QDir>
#include <QMessageBox>



using namespace d20;



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}



MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::saveTabContent(int index)
{
    CampaignMainWindow* tab = qobject_cast<CampaignMainWindow*>(ui->tabWidget->widget(index));
    if (!tab || !tab->campaign() || !tab->campaign()->isDirty()) return;

    QString file_path = tab->campaign()->filePath();
    if (!tab->campaign()->fileInfo().exists())
    {
        file_path = QFileDialog::getSaveFileName(this, tr("Save Campaign"), QDir::currentPath(), tr("Xml Files (*.xml)"));
    }

    if (file_path.isEmpty())
    {
        return;
    }


    tab->updateCampaign();
    tab->campaign()->setFilePath(file_path);
    Serializable::save(tab->campaign());

    onChanged();

}


void MainWindow::onChanged()
{
    CampaignMainWindow* tab = qobject_cast<CampaignMainWindow*>(ui->tabWidget->widget(ui->tabWidget->currentIndex()));
    if (!tab || !tab->campaign()) return;

    QString tabText = "%1%2";

    tabText = tabText.arg(tab->campaign()->name()).arg(tab->campaign()->isDirty() ? "*" : "");
    ui->tabWidget->setTabText(ui->tabWidget->currentIndex(),tabText);
}


void MainWindow::on_actionNew_triggered()
{
    Campaign* c = Campaign::create();
    if (!c) return;


    CampaignMainWindow* tab = new CampaignMainWindow(ui->tabWidget);
    tab->setCampaign(c);
    ui->tabWidget->addTab(tab, c->name());

    connect(c, SIGNAL(sigChanged()), this, SLOT(onChanged()));
}



void MainWindow::on_actionOpen_triggered()
{
    QString file_name = QFileDialog::getOpenFileName(
            this, tr("Open Campaign"), QDir::currentPath(), tr("Xml Files (*.xml)"));

    if (file_name.isEmpty()) return;

    Campaign* c = Campaign::create();
    if (!c) return;

    if (!Serializable::open(c, file_name)) c->deleteLater();


    CampaignMainWindow* tab = new CampaignMainWindow(ui->tabWidget);
    tab->setCampaign(c);
    ui->tabWidget->addTab(tab, c->name());
}



void MainWindow::on_actionSave_triggered()
{
    saveTabContent(ui->tabWidget->currentIndex());
}



void MainWindow::on_actionQuit_triggered()
{

    while (ui->tabWidget->widget(0))
    {
        on_tabWidget_tabCloseRequested(0);
    }

    exit(0);
}



void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
    CampaignMainWindow* tab = qobject_cast<CampaignMainWindow*>(ui->tabWidget->widget(index));
    if (!tab) return;

    if (!tab->campaign() || !tab->campaign()->isDirty())
    {
        ui->tabWidget->removeTab(index);
        return;
    }

    if (QMessageBox::question(this,
                              tr("Campaign Changed"),
                              tr("Campaign '%1' has been changed. Do you want to save changes?").arg(tab->campaign()->name()),
                              QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
    {
        saveTabContent(index);
    }


    ui->tabWidget->removeTab(index);
}



void MainWindow::on_actionSave_As_triggered()
{
}



void MainWindow::on_actionXml_toggled(bool on)
{
    CampaignMainWindow* tab = qobject_cast<CampaignMainWindow*>(ui->tabWidget->currentWidget());
    if (!tab) return;

    tab->showXml(on);
}


void MainWindow::on_actionAttribute_triggered()
{
    CampaignMainWindow* tab = qobject_cast<CampaignMainWindow*>(ui->tabWidget->currentWidget());
    if (!tab) return;

    tab->createItem(d20Type_Attribute,tr("New Attribute"));
}

void MainWindow::on_actionSkill_triggered()
{
    CampaignMainWindow* tab = qobject_cast<CampaignMainWindow*>(ui->tabWidget->currentWidget());
    if (!tab) return;

    tab->createItem(d20Type_Skill, tr("New Skill"));
}

void MainWindow::on_actionFeat_triggered()
{
    CampaignMainWindow* tab = qobject_cast<CampaignMainWindow*>(ui->tabWidget->currentWidget());
    if (!tab) return;

    tab->createItem(d20Type_Feat, tr("New Feat"));
}

void MainWindow::on_actionSpecial_triggered()
{
    CampaignMainWindow* tab = qobject_cast<CampaignMainWindow*>(ui->tabWidget->currentWidget());
    if (!tab) return;

    tab->createItem(d20Type_Special, tr("New Special Ability"));
}



void MainWindow::on_actionClose_Current_triggered()
{
    on_tabWidget_tabCloseRequested(ui->tabWidget->currentIndex());
}

void MainWindow::on_actionRename_triggered()
{
    CampaignMainWindow* tab = qobject_cast<CampaignMainWindow*>(ui->tabWidget->currentWidget());
    if (!tab) return;

    tab->renameCampaign();
}
